﻿Public Class frmPayroll
    Dim validator As PropertyValidator = New PropertyValidator
    Dim payrollManager As New PayrollManager
    Dim employeeInfoManager As New EmployeeInfoManager
    Private Sub frmPayroll_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ClearAllFields()
        PopulateEmployeeName(cboEmployeeName)
    End Sub
    Public Function ClearAllFields()
        validator.ResetControls(grpPayroll)
        btnSave.Text = "&Save"
        btnCancel.Text = "&Cancel"
        btnCompute.Text = "&Compute"
        Return 0
    End Function
    Public Function PopulateEmployeeName(ByVal cbo As ComboBox)

        Dim dataTable As New DataTable
        dataTable.Columns.Add("id")
        dataTable.Columns.Add("employeeName")
        Dim employeeList As List(Of EmployeeInfo) = employeeInfoManager.getAllEmployeesInfo()
        For Each employee As EmployeeInfo In employeeList
            dataTable.Rows.Add(employee.employee_id, employee.last_name & "," & employee.first_name)
        Next

        cbo.DataSource = dataTable
        cbo.DisplayMember = "employeeName"
        cbo.ValueMember = "id"
        cbo.SelectedIndex = -1

        Return (0)
    End Function


    'Private Sub cboEmployeeName_SelectedValueChanged(sender As System.Object, e As System.EventArgs) Handles cboEmployeeName.SelectedValueChanged
    '    If (Convert.ToInt16(cboEmployeeName.SelectedIndex > -1)) Then
    '        Dim empInfo As Employee = New EmployeeManager().retrieveEmployeeById(Convert.ToInt64(cboEmployeeName.SelectedValue.ToString()))
    '        txtSalary.Text = empInfo.salary
    '    End If
    'End Sub

    Private Sub btnCompute_Click(sender As System.Object, e As System.EventArgs) Handles btnCompute.Click
        If (Convert.ToInt16(cboEmployeeName.SelectedIndex > -1)) Then
            Dim empInfo As Employee = New EmployeeManager().retrieveEmployeeById(Convert.ToInt64(cboEmployeeName.SelectedValue.ToString()))
            txtSalary.Text = empInfo.salary
            Dim sssList As List(Of SSS) = payrollManager.getAllSSS()
            For Each sss As SSS In sssList
                If sss.min_salary <= empInfo.salary And empInfo.salary <= sss.max_salary Then
                    txtSSSAmount.Text = sss.employee_share.ToString()
                End If
            Next
            Dim pagibigList As List(Of PagIBIG) = payrollManager.getAllPagIBIG()
            For Each pagibig As PagIBIG In pagibigList
                If pagibig.min_salary <= empInfo.salary And empInfo.salary <= pagibig.max_salary Then
                    txtPagIBIGAmount.Text = pagibig.employee_share.ToString()
                End If
            Next
            Dim phiList As List(Of PhilHealth) = payrollManager.getAllPHI()
            For Each philhealth As PhilHealth In phiList
                If philhealth.min_salary <= empInfo.salary And empInfo.salary <= philhealth.max_salary Then
                    txtPhilHealthAmount.Text = philhealth.employee_share.ToString()
                End If
            Next
        End If
    End Sub
End Class